package com.returnsoft.resource.service;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.LocalBean;
import javax.ejb.Stateless;

import com.returnsoft.resource.dto.AreaDto;
import com.returnsoft.resource.dto.RecruiterDto;
import com.returnsoft.resource.eao.AreaEao;
import com.returnsoft.resource.eao.RecruiterEao;
import com.returnsoft.resource.entity.Area;
import com.returnsoft.resource.entity.Recruiter;
import com.returnsoft.resource.exception.ConversionExcepcion;
import com.returnsoft.resource.exception.EaoExcepcion;
import com.returnsoft.resource.exception.RecruitmentException;
import com.returnsoft.resource.util.Conversion;

/**
 * Session Bean implementation class RecruiterBean
 */
@Stateless
@LocalBean
public class RecruiterBean implements RecruiterInterface {

	@EJB
	private RecruiterEao recruiterEao;

	@EJB
	private AreaEao areaEao;

	@EJB
	private Conversion conv;

	/**
	 * Default constructor.
	 */
	public RecruiterBean() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<AreaDto> findAreas(Integer recruiterId)
			throws RecruitmentException {
		try {
			List<Area> areas = areaEao.findByRecruiter(recruiterId);
			List<AreaDto> areasDto = new ArrayList<AreaDto>();
			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);
			}
			return areasDto;
		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public RecruiterDto findByUser(Integer userId) throws RecruitmentException {
		try {

			Recruiter recruiter = recruiterEao.findRecruiterByUserId(userId);
			RecruiterDto recruiterDto = conv.fromRecruiter(recruiter);
			List<Area> areas = areaEao.findByRecruiter(recruiter.getId());

			List<AreaDto> areasDto = new ArrayList<AreaDto>();

			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);

			}

			recruiterDto.setAreasAvailable(areasDto);

			return recruiterDto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public RecruiterDto findById(Integer recruiterId)
			throws RecruitmentException {
		try {

			Recruiter recruiter = recruiterEao.findRecruiterById(recruiterId);
			RecruiterDto recruiterDto = conv.fromRecruiter(recruiter);

			List<Area> areas = areaEao.findByRecruiter(recruiter.getId());

			List<AreaDto> areasDto = new ArrayList<AreaDto>();

			for (Area area : areas) {
				AreaDto areaDto = conv.fromArea(area);
				areasDto.add(areaDto);

			}

			recruiterDto.setAreasAvailable(areasDto);

			return recruiterDto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public RecruiterDto addRecruiter(RecruiterDto recruiterDto)
			throws RecruitmentException {
		try {

			Recruiter recruiter = conv.toRecruiter(recruiterDto);
			System.out.println("en el bean");

			// System.out.println(recruiter.getDepartments().size());

			recruiter = recruiterEao.addRecruiter(recruiter);

			recruiterDto = conv.fromRecruiter(recruiter);

			return recruiterDto;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (ConversionExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public List<RecruiterDto> findRecruiters(List<Integer> areasId,
			Boolean areaNull, String name, String documentNumber)
			throws RecruitmentException {
		try {

			List<RecruiterDto> entities = recruiterEao.findRecruiters(areasId,
					areaNull, name, documentNumber);

			return entities;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

	@Override
	public List<RecruiterDto> findAll() throws RecruitmentException {
		try {

			List<RecruiterDto> entities = recruiterEao.findAll();

			return entities;

		} catch (EaoExcepcion e) {
			throw new RecruitmentException(e.getMessage());
		} catch (Exception e) {
			e.printStackTrace();
			throw new RecruitmentException(e.getMessage());
		}
	}

}
